package com.andova.empty;

import android.app.Activity;

import com.andova.empty.api.IEmptyActivityOption;
import com.andova.empty.api.IEmptyApplicationOption;
import com.andova.empty.api.IEmptyManager;
import com.andova.empty.app.EmptyLifecycle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

/**
 * Created by Administrator on 2018-02-07.
 *
 * @author kzaxil
 * @since 1.0.0
 */
public class EmptyManager implements IEmptyManager {
    private EmptyLifecycle mLifecycle;
    private IEmptyApplicationOption mEmptyOption;

    private Map<Activity, List<Integer>> mValidIdList = new HashMap<>(1);
    private Map<Activity, IEmptyActivityOption> mEmptyOptionMap = new HashMap<>(1);

    private boolean mIsInit = false;

    /**
     * 默认情况下，布局中所有的{@link android.widget.RelativeLayout}会转换为{@link com.andova.empty.widget.EmptyRelativeLayout}，
     * 所有的{@link android.widget.LinearLayout}会转换为{@link com.andova.empty.widget.EmptyLinearLayout}，
     * 通过这个接口，可让忽略相应ID控件的转换
     *
     * @param list 子元素id集合
     */
    @Override
    public IEmptyManager validIdList(Activity activity, List<Integer> list) {
        if (!mIsInit) return this;
        mValidIdList.put(activity, list);
        return this;
    }

    @Override
    public List<Integer> validIdList(Activity activity) {
        return mValidIdList.get(activity);
    }

    @Override
    public IEmptyActivityOption activityOption(Activity activity) {
        return mEmptyOptionMap.get(activity);
    }

    @Override
    public IEmptyManager activityOption(Activity activity, IEmptyActivityOption layoutOption) {
        if (!mIsInit) return this;
        mEmptyOptionMap.put(activity, layoutOption);
        return this;
    }

    @Override
    public IEmptyApplicationOption appOption() {
        return mEmptyOption;
    }

    @Override
    public void errorView() {
        if (mLifecycle == null) return;
        mLifecycle.showErrorView();
    }

    @Override
    public void loadingView() {
        if (mLifecycle == null) return;
        mLifecycle.showLoadingView();
    }

    @Override
    public void contentView() {
        if (mLifecycle == null) return;
        mLifecycle.showContentView();
    }

    @Override
    public void emptyView() {
        if (mLifecycle == null) return;
        mLifecycle.showEmptyView();
    }

    @Override
    public void layoutView(Activity activity, @LayoutRes int layoutId) {
        activity.setContentView(layoutId);
    }

    @Override
    public void errorView(Activity activity, @LayoutRes int layoutId) {
        activity.setContentView(layoutId);
        errorView();
    }

    @Override
    public void loadingView(Activity activity, @LayoutRes int layoutId) {
        activity.setContentView(layoutId);
        loadingView();
    }

    @Override
    public void contentView(Activity activity, @LayoutRes int layoutId) {
        activity.setContentView(layoutId);
        contentView();
    }

    @Override
    public void removeOption(Activity activity) {
        mEmptyOptionMap.remove(activity);
    }

    @Override
    public void removeIdList(Activity activity) {
        mValidIdList.remove(activity);
    }

    @Override
    public void emptyView(Activity activity, @LayoutRes int layoutId) {
        activity.setContentView(layoutId);
        emptyView();
    }

    @Override
    public EmptyLifecycle initialize(@NonNull IEmptyApplicationOption emptyOption) {
        this.mEmptyOption = emptyOption;
        mLifecycle = new EmptyLifecycle();
        mIsInit = true;
        return mLifecycle;
    }
}
